//
//  BBControllerNodeView.m
//  xNose
//
//  Created by ben smith on 12/09/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBControllerNodeView.h"
#import "BBNode.h"
#import "BBDefaultController.h"
#import "BBImageButtonFactory.h"
#import "BBNodeInfoView.h"
#import "BBNodeValuesView.h"
#import "BBRadialControlView.h"


@implementation BBControllerNodeView

-(void)addInfoButton
{
}

-(void)setupControls
{
//	[self setWantsLayer:YES];
	float buttonSize = 30;
	NSRect buttonRect = NSMakeRect(NSMinX(self.bounds), NSMaxY(self.bounds) - buttonSize, buttonSize, buttonSize);
	
	NSButton * button = [BBImageButtonFactory closeButtonInRect:buttonRect];
	[button setTarget:self];
	[button setAction:@selector(buttonClick)];
	[button setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
	[self addSubview:button];
		
	float textPerc = 0.45;
	float controlPerc = 1.0 - textPerc;
	
	if ([self.node hasChildren]) {
		textPerc = 1.0;
	}
	
	float titleHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTitleBarHeight"];
	NSRect containRect = [backgroundRect bounds];
	containRect.size.height = floor(containRect.size.height) - titleHeight;
	
	NSRect textblock = containRect;
	textblock.size.width *= textPerc;
	textblock = NSInsetRect(textblock, 10, 10);
	// now chop it down a bit further, want it to be the upper 3/5 of the chunk
	textblock.origin.y += NSHeight(containRect) * (2.0/5.0);
	textblock.size.height -= NSHeight(containRect) * (2.0/5.0);
	
	nodeInfo = [[BBNodeInfoView alloc] initWithFrame:textblock];
	[nodeInfo setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable | NSViewMinXMargin | NSViewMaxXMargin |NSViewMinYMargin | NSViewMaxYMargin];
	if ([node isCollection]) [nodeInfo setShowsSubtitle:YES];
	[self addSubview:nodeInfo];
	
	// now add the values view
	NSRect valueRect = containRect;
	valueRect.size.width *= textPerc;
	valueRect = NSInsetRect(valueRect, 10, 10);
	valueRect.size.height -= NSHeight(containRect) * (3.0/5.0);
	
	nodeValues = [[BBNodeValuesView alloc] initWithFrame:valueRect];
	[nodeValues setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable | NSViewMinXMargin | NSViewMaxXMargin |NSViewMinYMargin | NSViewMaxYMargin];
	if (([[NSUserDefaults standardUserDefaults] boolForKey:@"BBCanEditNodes"]) && (![self.node hasChildren])) [nodeValues setIsEditable:YES];
	[self addSubview:nodeValues];
	
	if ([self.node hasChildren]) return; /// get out now, do not want the radial control
	
	NSRect controlblock = containRect;
	controlblock.size.width *= controlPerc;
	controlblock.origin.x += (NSWidth(containRect) * textPerc);
	controlblock = NSInsetRect(controlblock, 15, 10);
	BBRadialControlView * radialControl = [[BBRadialControlView alloc] initWithFrame:controlblock];
	[radialControl setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable | NSViewMinXMargin | NSViewMaxXMargin |NSViewMinYMargin | NSViewMaxYMargin];
	[radialControl setDelegate:self];
	[self addSubview:radialControl];
	[radialControl release];
}

-(void)setNode:(BBNode*)aNode
{
	[aNode retain];
	[node release];
	node = aNode;

	if (node != nil)
		[titleField setStringValue:self.node.title];
	
	[self setupControls];
	
	[nodeInfo setNode:aNode];
	[nodeValues setNode:aNode];
}

-(void)drawNode
{
	if ((NSWidth(self.bounds) < 10) || (NSHeight(self.bounds) < 10)) return;
	[self setupBackgroundRect];
	[self.backgroundGradient drawInBezierPath:backgroundRect angle:90.0];		
	
	NSRect containRect = [backgroundRect bounds];
	float titleHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTitleBarHeight"];
	
	[[NSColor grayColor] set];
	[backgroundRect stroke];
	[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(containRect), NSMaxY(containRect) - titleHeight) toPoint:NSMakePoint(NSMaxX(containRect), NSMaxY(containRect) - titleHeight)];
	
	//[node.title drawInRect:[self textRect] withAttributes:textAttributes];
	
  //super duper sketchy
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float buttonSize = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBInfoButtonSize"];
	NSRect contentRect = [backgroundRect bounds];
	float yOffset = NSMaxY(contentRect) - (buttonSize) - borderHeight;
	NSRect buttonRect = NSMakeRect(NSMaxX(contentRect) - buttonSize - (borderHeight * 2.0), yOffset, buttonSize, buttonSize);
	
	[infoButton setFrame:buttonRect];	
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
	NSGraphicsContext *nsGraphicsContext;
	nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx
																																 flipped:NO];
	[NSGraphicsContext saveGraphicsState];	
	[NSGraphicsContext setCurrentContext:nsGraphicsContext];
	//	[BBNodeDrawingFactory drawLeafNode:self.node];
	[self drawNode];
	[NSGraphicsContext restoreGraphicsState];
}


-(void)buttonClick
{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBCloseControllerNotification" object:self.node];	
}

-(NSRect)textRect
{
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float fieldHeight  = [BBDefaultController fontHeight:[self font]];
	float textInset = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTextInsetWidth"];
	NSRect content = [backgroundRect bounds];
	
	NSRect textFrame = NSMakeRect(NSMinX(content) + (textInset * 2.0), NSMaxY(content) - fieldHeight - (borderHeight * 2), NSWidth(content) - (textInset * 4.0), fieldHeight);
	
	return textFrame;	
}


-(void)setupBackgroundRect
{
	if ((NSWidth(self.bounds) < 10) || (NSHeight(self.bounds) < 10)) return;

	if ((NSWidth(self.bounds) < 40) || (NSHeight(self.bounds) < 40)) {
		self.backgroundRect = [NSBezierPath bezierPathWithRect:self.bounds];
		return;
	}
	
	NSRect containRect = NSInsetRect(self.bounds, 15, 15);
	containRect.origin.x = floor(containRect.origin.x) + 0.5;
	containRect.origin.y = floor(containRect.origin.y) + 0.5;
	containRect.size.width = floor(containRect.size.width);
	containRect.size.height = floor(containRect.size.height);
	
	self.backgroundRect = [NSBezierPath bezierPathWithRoundedRect:containRect xRadius:10 yRadius:10];
}

-(void)setupTextAttributes
{
		titleField = [[NSTextField alloc] initWithFrame:[self textRect]];
		[titleField setAlignment:NSCenterTextAlignment];
		[titleField setFont:[self font]];
		[titleField setTextColor:[self fontColor]];
		[titleField setDrawsBackground:NO];
		[titleField setBordered:NO];
		[titleField setSelectable:NO];
		[titleField setEditable:NO];
		[titleField setTarget:self];
		[titleField setAction:@selector(fieldDone:)];
		[titleField setDelegate:self];
		//[titleField setAutoresizingMask:NSViewMinYMargin];
		[self addSubview:titleField];
	NSLog(@"this is working");
	if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BBCanEditNodes"]) {
		[titleField setSelectable:YES];
		[titleField setEditable:YES];		
	}
}

-(void)otherMouseDragged:(NSEvent*)theEvent
{
	[self mouseDragged:theEvent];
}

-(void)otherMouseDown:(NSEvent*)theEvent
{
	[self mouseDown:theEvent];
}


-(void)mouseDown:(NSEvent*)theEvent
{
	mouseDown = [theEvent locationInWindow];	
}

-(void)mouseDragged:(NSEvent*)theEvent
{
	NSPoint newMouse = [theEvent locationInWindow];
	NSRect  frame = [self frame];
	frame.origin.x += newMouse.x - mouseDown.x;
	frame.origin.y += newMouse.y - mouseDown.y;
	[self setFrame:frame];
	mouseDown = newMouse;
}


- (void) dealloc
{
	[nodeInfo release];
	[nodeValues release];
	[super dealloc];
}


@end
